package com.seven.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.codehaus.jackson.map.ObjectMapper;

import com.seven.entity.Users;
import com.seven.service.AUsersService;

@WebServlet("/user")
public class AUsersServlet extends BaseServlet{

	AUsersService usersService = new AUsersService();


	public void login(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		String users_username = request.getParameter("users_username");
		String users_password = request.getParameter("users_password");

		System.out.println(users_username+"==============="+users_password);
		Users users = new Users();
		users.setUsers_username(users_username);
		users.setUsers_password(users_password);
		Users user = usersService.selectByUsers_username(users);
		String re="";
		if (user!=null){
			ObjectMapper mapper = new ObjectMapper();
			re ="{\"state\":\"ok\",\"user\":"+mapper.writeValueAsString(user)+"}";
		}else {
			re ="{\"state\":\"密码错误\"}";
		}
		System.out.println(re);
		out.write(re);
		out.close();
	}

	public void registe(HttpServletRequest request,HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		String unamedt = request.getParameter("unamedt");
		String unumberedt = request.getParameter("unumberedt");
		String upasswordedt1 = request.getParameter("upasswordedt1");
		Users users = new Users();
		users.setUsers_username(unumberedt);
		users.setUsers_password(upasswordedt1);
		users.setUsers_name(unamedt);
		int i = usersService.addUsers(users);
		System.out.println(unamedt+" "+unumberedt+" "+upasswordedt1);
		if (i>0){
			out.print(true);
		}else {
			out.print(false);
		}
	}
}
